/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    DataSelector.cs

Abstract: 

    Form to take user input for source of machines to monitor
     
--*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AddIn.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;
using System.Globalization;
using Visio = Microsoft.Office.Interop.Visio;

namespace NRTMAddIn
{
    public partial class DataSelector : AddIn.Windows.Forms.ExteriorWizardPage
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        public DataSelector()
        {
            InitializeComponent();
            this.TitleLabel.Text = Global.GetResourceString("DataSelectorSource_Label");
            this.rbSources3.Text = Global.GetResourceString("DataSelectorOption3_Radio");
            this.rbSources2.Text = Global.GetResourceString("DataSelectorOption2_Radio");
            this.rbSources1.Text = Global.GetResourceString("DataSelectorOption1_Radio");

        }
        /// <summary>
        /// Enables Next Button
        /// </summary>
        /// <returns>
        /// True if enables next button 
        ///</returns>
        protected override bool OnSetActive()
        {
            if (!base.OnSetActive())
                return false;

            // Enable only the Next button on the this page    
            Wizard.SetWizardButtons(WizardButtons.Next);
            return true;
        }

        /// <summary>
        /// Code to be executed before form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataSelector_Load(object sender, EventArgs e)
        {
            System.IO.Stream xsdResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NRTMAddIn.Images.data.bmp");
            this.WatermarkPicture.Image = System.Drawing.Image.FromStream(xsdResourceStream);

            //Check the option selected by user on previous run
            if (Global.IPOption == Global.InputDataOption.SCOM)
            {
                rbSources1.Checked = true;
            }
            else if (Global.IPOption == Global.InputDataOption.Excel)
            {
                rbSources2.Checked = true;
            }

            //Disable active page option
            rbSources3.Enabled = false;

            //Enable Active Page option only when Active Page present with shapes
            if (Global.VisioApp.ActivePage != null && Global.VisioApp.ActivePage.Shapes.Count != 0)
            {
                foreach (Visio.Shape shape in Global.VisioApp.ActivePage.Shapes)
                {
                    //Check for server shapes with type = 2
                    if (ConfigureServerShape.ValidNetworkShape(shape))
                    {
                        Visio.Cell cellNtwrkName = shape.get_Cells("Prop.NetworkName.Value");
                        Visio.Cell cellIpAddress = shape.get_Cells("Prop.IPAddress.Value");

                        string SerName = cellNtwrkName.FormulaU.Replace("\"", "").Trim().ToUpper(CultureInfo.CurrentCulture);
                        string IPAddr = cellIpAddress.FormulaU.Replace("\"", "").Trim().ToUpper(CultureInfo.CurrentCulture);

                        //Check if shape configured with server name and ip address
                        if (!string.IsNullOrEmpty(SerName) || !string.IsNullOrEmpty(IPAddr))
                        {
                            //Enable active page option
                            rbSources3.Enabled = true;

                            if (Global.IPOption == Global.InputDataOption.ActivePage)
                            {
                                //If it was the option used last time mark it checked
                                rbSources3.Checked = true;
                            }
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Action to be performed based on Monitor Option
        /// </summary>
        /// <returns></returns>
        protected override string OnWizardNext()
        {
            try
            {
                //OpsMgr Selected
                if (rbSources1.Checked == true)
                {
                    Global.IPOption = Global.InputDataOption.SCOM;
                    return "OpsMgrConnection";
                }
                //Excel Selected
                else if (rbSources2.Checked == true)
                {
                    if (!Global.ExcelInstalled())
                    {
                        MessageBox.Show(Global.GetResourceString("Excel_Install"), 
                            Global.GetResourceString("NRTM_DataSelect_MsgBox_Title"),
                            MessageBoxButtons.OK, MessageBoxIcon.Error,
                            MessageBoxDefaultButton.Button1);
                        return "DataSelector";
                    }
                    else
                    {
                        //Get a excel file name from user
                        this.openFileDialog1.FileName = "";
                        this.openFileDialog1.InitialDirectory = Global.AppPath();
                        this.openFileDialog1.CheckFileExists = true;
                        this.openFileDialog1.CheckPathExists = true;
                        this.openFileDialog1.Multiselect = false;
                        this.openFileDialog1.Filter = Global.GetResourceString("Excel_FileDialog_Filter");
                        DialogResult ExcelFileSelect = this.openFileDialog1.ShowDialog();
                        if (ExcelFileSelect == DialogResult.OK)
                        {
                            Global.ExcelFileName = this.openFileDialog1.FileName;
                            Global.IPOption = Global.InputDataOption.Excel;
                            return "MachineSelector";
                        }
                        else 
                        {
                            return "DataSelector";
                        }
                    }
                }
                //Active Document Selected
                else
                {
                    if (Global.VisioApp.ActiveDocument != null)
                    {
                        Document nrtmDoc = Application.GetDocumentObject(Global.VisioApp.ActiveDocument);
                        if (nrtmDoc != null && nrtmDoc.diskMonitoringOn)
                        {                            
                            MessageBox.Show(Global.GetResourceString("DataSelectMonitoringOn"), 
                                Global.GetResourceString("NRTM_DataSelect_MsgBox_Title"),
                                MessageBoxButtons.OK, MessageBoxIcon.Information,
                                MessageBoxDefaultButton.Button1);
                            return "DataSelector";
                        }
                        if (nrtmDoc != null && nrtmDoc.diagramGenerationOn)
                        {                            
                            MessageBox.Show(Global.GetResourceString("DataSelectDiagramGenerationOn"), 
                                Global.GetResourceString("NRTM_DataSelect_MsgBox_Title"),
                                MessageBoxButtons.OK, MessageBoxIcon.Information,
                                MessageBoxDefaultButton.Button1);
                            return "DataSelector";
                        }
                        Global.IPOption = Global.InputDataOption.ActivePage;
                        return "MachineSelector";
                    }
                    else
                    {
                        return "DataSelector";
                    }
                }
            }
            catch (NrtmException ex)
            {
                //To catch and display exceptions from sub functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);

                return null;
            }
            catch (Exception ex)
            {
                //To catch exceptions from current functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                             Global.GetResourceString("Exception_UsrMsg"), ex);

                return null;
            }
        }
    }
}

